//
//  ChangeIconViewController.swift
//  XMenSwift
//
//  Created by Jason on 2023/8/10.
//

import UIKit

struct IconItem {
    var iconName = ""
    var imageName = ""
    var displayName = ""
}

class ChangeIconViewController: BaseTableViewController {

    var iconData = [IconItem(iconName: "1", imageName: "Icon1-40", displayName: "默认"),
                    IconItem(iconName: "2", imageName: "Icon2-40", displayName: "Icon2"),
                    IconItem(iconName: "3", imageName: "Icon3-40", displayName: "Icon3")]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = []
        initUI()
    }
    func initUI() {
        self.setupTableView(with: .grouped)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
    }
}

extension ChangeIconViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return iconData.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
        let iconItem = iconData[indexPath.row]
        cell?.imageView?.image = UIImage(named: iconItem.imageName)
        cell?.textLabel?.text = iconItem.displayName
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 30)
        return cell!
    }
}

extension ChangeIconViewController {
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        var iconName: String? = nil
        let iconItem = iconData[indexPath.row]
        if !iconItem.iconName.isEmpty {
            iconName = iconItem.iconName
        }
        var tempType = AppIconType.default
        AppIconType.allCases.forEach { type in
            if type.rawValue == Int(iconName ?? "0") {
                tempType = type
            }
        }
        AppIconManage.changeAppIcon(type: tempType)
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
}
